1   package net.sf.jmimemagic;
2   
3   import junit.framework.*;
4   import java.io.File;
5   
6   public class MagicTest extends TestCase {
7   	private static String gifFile = "test_docs/test.gif";
8   	private static String pngFile = "test_docs/test.png";
9   	private static String jpgFile = "test_docs/test.jpg";
10  	private static String textFile = "test_docs/test.txt";
11  	private static String word2KFile = "test_docs/test_word_2000.doc";
12  	private static String word95File = "test_docs/test_word_6.0_95.doc";
13  	private static String rtfFile = "test_docs/test.rtf";
14  	private static String excel2KFile = "test_docs/test_excel_2000.xls";
15  	private static String pdfFile = "test_docs/test.pdf";
16  	private static String psFile = "test_docs/test.ps";
17  	private static String javaClass12File = "test_docs/test_1.2.class";
18  	private static String javaClass13File = "test_docs/test_1.3.class";
19  	private static String javaClass14File = "test_docs/test_1.4.class";
20  	private static String mp3_128_44_jstereoFile = "test_docs/test_128_44_jstereo.mp3";
21  	private static String wavFile = "test_docs/test.wav";
22  
23  	public static void main(String args[]) {
24  		junit.textui.TestRunner.run(MagicTest.class);
25  	}
26  
27  	// IMAGE TESTS
28  
29  	public void testGIF() {
30  		System.out.print("\ntesting GIF image...");
31  		try {
32  			MagicMatch match = Magic.getMagicMatch(new File(gifFile), true, false);
33  			if (match != null) {
34  				assertEquals("image/gif", match.getMimeType());
35  			} else {
36  				System.out.print("failed");
37  				fail("no match in testGIF()");
38  			}
39  			System.out.print("ok");
40  		} catch (Exception e) {
41  			e.printStackTrace();
42  			fail("exception in testGIF(). message: " + e);
43  		} catch (Error e) {
44  			e.printStackTrace();
45  			fail("error in testGIF(). message: " + e.getMessage());
46  		}
47  	}
48  
49  	public void testPNG() {
50  		System.out.print("\ntesting PNG image...");
51  		try {
52  			MagicMatch match = Magic.getMagicMatch(new File(pngFile), true, false);
53  			if (match != null) {
54  				assertEquals("image/png", match.getMimeType());
55  			} else {
56  				System.out.print("failed");
57  				fail("no match in testPNG()");
58  			}
59  			System.out.print("ok");
60  		} catch (Exception e) {
61  			e.printStackTrace();
62  			fail("exception in testPNG(). message: " + e);
63  		} catch (Error e) {
64  			e.printStackTrace();
65  			fail("error in testPNG(). message: " + e.getMessage());
66  		}
67  	}
68  
69  	public void testJPEG() {
70  		System.out.print("\ntesting JPEG image...");
71  		try {
72  			MagicMatch match = Magic.getMagicMatch(new File(jpgFile), true, false);
73  			if (match != null) {
74  				assertEquals("image/jpeg", match.getMimeType());
75  			} else {
76  				System.out.print("failed");
77  				fail("no match in testJPEG()");
78  			}
79  			System.out.print("ok");
80  		} catch (Exception e) {
81  			e.printStackTrace();
82  			fail("exception in testJPEG(). message: " + e);
83  		} catch (Error e) {
84  			e.printStackTrace();
85  			fail("error in testJPEG(). message: " + e.getMessage());
86  		}
87  	}
88  
89  	// DOCUMENT TESTS
90  
91  	public void testText() {
92  		System.out.print("\ntesting Plain Text Document...");
93  		try {
94  			MagicMatch match = Magic.getMagicMatch(new File(textFile), true, false);
95  			if (match != null) {
96  				assertEquals("text/plain", match.getMimeType());
97  			} else {
98  				System.out.print("failed");
99  				fail("no match in testText()");
100 			}
101 			System.out.print("ok");
102 		} catch (Exception e) {
103 			e.printStackTrace();
104 			fail("exception in testText(). message: " + e);
105 		} catch (Error e) {
106 			e.printStackTrace();
107 			fail("error in testText(). message: " + e.getMessage());
108 		}
109 	}
110 
111 	public void testWord2K() {
112 		System.out.print("\ntesting Word 2000 Document...");
113 		try {
114 			MagicMatch match = Magic.getMagicMatch(new File(word2KFile), true, false);
115 			if (match != null) {
116 				assertEquals("application/msword", match.getMimeType());
117 			} else {
118 				System.out.print("failed");
119 				fail("no match in testWord2K()");
120 			}
121 			System.out.print("ok");
122 		} catch (Exception e) {
123 			e.printStackTrace();
124 			fail("exception in testWord2K(). message: " + e);
125 		} catch (Error e) {
126 			e.printStackTrace();
127 			fail("error in testWord2K(). message: " + e.getMessage());
128 		}
129 	}
130 
131 	public void testWord95() {
132 		System.out.print("\ntesting Word 95 Document...");
133 		try {
134 			MagicMatch match = Magic.getMagicMatch(new File(word95File), true, false);
135 			if (match != null) {
136 				assertEquals("application/msword", match.getMimeType());
137 			} else {
138 				System.out.print("failed");
139 				fail("no match in testWord95()");
140 			}
141 			System.out.print("ok");
142 		} catch (Exception e) {
143 			e.printStackTrace();
144 			fail("exception in testWord95(). message: " + e);
145 		} catch (Error e) {
146 			e.printStackTrace();
147 			fail("error in testWord95(). message: " + e.getMessage());
148 		}
149 	}
150 
151 	public void testRTF() {
152 		System.out.print("\ntesting RTF Document...");
153 		try {
154 			MagicMatch match = Magic.getMagicMatch(new File(rtfFile), true, false);
155 			if (match != null) {
156 				assertEquals("text/rtf", match.getMimeType());
157 			} else {
158 				System.out.print("failed");
159 				fail("no match in testRTF()");
160 			}
161 			System.out.print("ok");
162 		} catch (Exception e) {
163 			e.printStackTrace();
164 			fail("exception in testRTF(). message: " + e);
165 		} catch (Error e) {
166 			e.printStackTrace();
167 			fail("error in testRTF(). message: " + e.getMessage());
168 		}
169 	}
170 
171 	public void testExcel2K() {
172 		System.out.print("\ntesting Excel 2000 Document...");
173 		try {
174 			MagicMatch match = Magic.getMagicMatch(new File(excel2KFile), true, false);
175 			if (match != null) {
176 				assertEquals("application/msword", match.getMimeType());
177 			} else {
178 				System.out.print("failed");
179 				fail("no match in testExcel2K()");
180 			}
181 			System.out.print("ok");
182 		} catch (Exception e) {
183 			e.printStackTrace();
184 			fail("exception in testExcel2K(). message: " + e);
185 		} catch (Error e) {
186 			e.printStackTrace();
187 			fail("error in testExcel2K(). message: " + e.getMessage());
188 		}
189 	}
190 
191 	// OTHER
192 
193 	public void testPDF() {
194 		System.out.print("\ntesting PDF Document...");
195 		try {
196 			MagicMatch match = Magic.getMagicMatch(new File(pdfFile), true, false);
197 			if (match != null) {
198 				assertEquals("application/pdf", match.getMimeType());
199 			} else {
200 				System.out.print("failed");
201 				fail("no match in testPDF()");
202 			}
203 			System.out.print("ok");
204 		} catch (Exception e) {
205 			e.printStackTrace();
206 			fail("exception in testPDF(). message: " + e);
207 		} catch (Error e) {
208 			e.printStackTrace();
209 			fail("error in testPDF(). message: " + e.getMessage());
210 		}
211 	}
212 
213 	public void testPostscript() {
214 		System.out.print("\ntesting Postscript Document...");
215 		try {
216 			MagicMatch match = Magic.getMagicMatch(new File(psFile), true, false);
217 			if (match != null) {
218 				assertEquals("application/postscript", match.getMimeType());
219 			} else {
220 				System.out.print("failed");
221 				fail("no match in testPostscript()");
222 			}
223 			System.out.print("ok");
224 		} catch (Exception e) {
225 			e.printStackTrace();
226 			fail("exception in testPostscript(). message: " + e);
227 		} catch (Error e) {
228 			e.printStackTrace();
229 			fail("error in testPostscript(). message: " + e.getMessage());
230 		}
231 	}
232 
233 	public void testJavaClass12() {
234 		System.out.print("\ntesting Java Class File (v1.2)...");
235 		try {
236 			MagicMatch match = Magic.getMagicMatch(new File(javaClass12File), true, false);
237 			if (match != null) {
238 				assertEquals("application/java", match.getMimeType());
239 			} else {
240 				System.out.print("failed");
241 				fail("no match in testJavaClass12()");
242 			}
243 			System.out.print("ok");
244 		} catch (Exception e) {
245 			e.printStackTrace();
246 			fail("exception in testJavaClass12(). message: " + e);
247 		} catch (Error e) {
248 			e.printStackTrace();
249 			fail("error in testJavaClass12(). message: " + e.getMessage());
250 		}
251 	}
252 
253 	public void testJavaClass13() {
254 		System.out.print("\ntesting Java Class File (v1.3)...");
255 		try {
256 			MagicMatch match = Magic.getMagicMatch(new File(javaClass13File), true, false);
257 			if (match != null) {
258 				assertEquals("application/java", match.getMimeType());
259 			} else {
260 				System.out.print("failed");
261 				fail("no match in testJavaClass13()");
262 			}
263 			System.out.print("ok");
264 		} catch (Exception e) {
265 			e.printStackTrace();
266 			fail("exception in testJavaClass13(). message: " + e);
267 		} catch (Error e) {
268 			e.printStackTrace();
269 			fail("error in testJavaClass13(). message: " + e.getMessage());
270 		}
271 	}
272 
273 	public void testJavaClass14() {
274 		System.out.print("\ntesting Java Class File (v1.4)...");
275 		try {
276 			MagicMatch match = Magic.getMagicMatch(new File(javaClass14File), true, false);
277 			if (match != null) {
278 				assertEquals("application/java", match.getMimeType());
279 			} else {
280 				System.out.print("failed");
281 				fail("no match in testJavaClass14()");
282 			}
283 			System.out.print("ok");
284 		} catch (Exception e) {
285 			e.printStackTrace();
286 			fail("exception in testJavaClass14(). message: " + e);
287 		} catch (Error e) {
288 			e.printStackTrace();
289 			fail("error in testJavaClass14(). message: " + e.getMessage());
290 		}
291 	}
292 
293 	public void testMP3() {
294 		System.out.print("\ntesting MPEG Layer 3...");
295 		try {
296 			MagicMatch match = Magic.getMagicMatch(new File(mp3_128_44_jstereoFile), true, false);
297 			if (match != null) {
298 				assertEquals("audio/mp3", match.getMimeType());
299 			} else {
300 				System.out.print("failed");
301 				fail("no match in testMP3()");
302 			}
303 			System.out.print("ok");
304 		} catch (Exception e) {
305 			e.printStackTrace();
306 			fail("exception in testMP3(). message: " + e);
307 		} catch (Error e) {
308 			e.printStackTrace();
309 			fail("error in testMP3(). message: " + e.getMessage());
310 		}
311 	}
312 
313 	public void testWave() {
314 		System.out.print("\ntesting WAVE...");
315 		try {
316 			MagicMatch match = Magic.getMagicMatch(new File(wavFile), true, false);
317 			if (match != null) {
318 				assertEquals("RIFF (little-endian) data", match.getDescription());
319 			} else {
320 				System.out.print("failed");
321 				fail("no match in testWave()");
322 			}
323 			assertTrue(match.descriptionMatches("Microsoft PCM"));
324 			assertTrue(match.descriptionMatches("stereo"));
325 			assertTrue(match.mimeTypeMatches("application/x-wav"));
326 			System.out.print("ok");
327 		} catch (Exception e) {
328 			e.printStackTrace();
329 			fail("exception in testWave(). message: " + e);
330 		} catch (Error e) {
331 			e.printStackTrace();
332 			fail("error in testWave(). message: " + e.getMessage());
333 		}
334 	}
335 }